package com.gimi.cloud.bbp.task.controller;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.task.ScheduleJobDTO;
import com.gimi.cloud.bbp.task.service.ScheduleJobService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务相关controller
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-02-06
 **/
@RestController
@RequestMapping("/task")
public class ScheduleJobController {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobController.class);
    private ScheduleJobService scheduleJobService;

    @Autowired
    public ScheduleJobController(ScheduleJobService scheduleJobService) {
        this.scheduleJobService = scheduleJobService;
    }

    /**
     * 获取定时任务分页列表
     * @param request
     * @param response
     * @param dto 查询参数
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response, @RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        List<ScheduleJobDTO> scheduleJobs = scheduleJobService.queryList(dto,true);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,new PageInfo<>(scheduleJobs));
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 加载定时任务数据
     * @param request
     * @param response
     * @param dto
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws Exception{
        Response resp = new Response();
        dto = scheduleJobService.load(dto,dto.getJobId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("scheduleJob",dto);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 添加定时任务
     * @param request
     * @param response
     * @param dto 待添加的定时任务
     * @throws Exception
     */
    @RequestMapping("/add")
    public Response save(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        if(null != dto.getJobId()){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"定时任务id不为空，添加失败");
            return resp;
        }
        scheduleJobService.save(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"添加成功");
        return resp;
    }
    /**
     * 更新定时任务
     * @param request
     * @param response
     * @param dto 待更新的定时任务
     * @throws Exception
     */
    @RequestMapping("/update")
    public Response update(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        if(null == dto.getJobId()){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"定时任务id为空，更新失败");
            return resp;
        }
        scheduleJobService.update(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"添加成功");
        return resp;
    }

    /**
     * 删除定时任务
     * @param request
     * @param response
     * @param dto 待删除的定时任务
     * @throws Exception
     */
    @RequestMapping("/delete")
    public Response delete(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        if(null == dto.getJobId()){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"定时任务id为空，删除失败");
            return resp;
        }
        scheduleJobService.delete(dto,dto.getJobId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }
    /**
     * 执行定时任务
     * @param request
     * @param response
     * @param dto 待执行定时任务
     * @throws Exception
     */
    @RequestMapping("/run")
    public Response run(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        if(null == dto.getJobId()){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"定时任务id为空，执行失败");
            return resp;
        }
        scheduleJobService.runJob(dto,new Integer[]{dto.getJobId()});
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"执行成功");
        return resp;
    }

    /**
     * 暂停定时任务
     * @param request
     * @param response
     * @param dto 待暂停的定时任务
     * @throws Exception
     */
    @RequestMapping("/pause")
    public Response pause(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        if(null == dto.getJobId()){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"定时任务id为空，暂停失败");
            return resp;
        }
        scheduleJobService.pauseJob(dto,new Integer[]{dto.getJobId()});
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"暂停成功");
        return resp;
    }

    /**
     * 启动定时任务
     * @param request
     * @param response
     * @param dto 待启动的定时任务
     * @throws Exception
     */
    @RequestMapping("/resume")
    public Response resume(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        if(null == dto.getJobId()){
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,"定时任务id为空，启动失败");
            return resp;
        }
        scheduleJobService.resumeJob(dto,new Integer[]{dto.getJobId()});
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"启动成功");
        return resp;
    }

    /**
     * 重持久化定时任务
     * @param request
     * @param response
     * @param dto 待重持久化的定时任务
     * @throws Exception
     */
    @RequestMapping("/recreate")
    public Response recreate(HttpServletRequest request,HttpServletResponse response,@RequestBody ScheduleJobDTO dto) throws  Exception{
        Response resp = new Response();
        scheduleJobService.recreateJob(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"持久化成功");
        return resp;
    }
}
